'use strict';
module.exports = {
	/**
	 * 计算平台总数据
	 */
	main: async (event) => {
		const db = uniCloud.database();
		const $ = db.command.aggregate;
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			_
		} = util;
		let {
			uid,
			todayStart,
			todayEnd,
			last_StartTime,
			last_EndTime,
			yesterday
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------


		//查询昨日充值总金额
		res.all_last_jrczje = await vk.baseDao.sum({
			dbName: "a_recharge",
			fieldName: "shifu_amount", // 需要求和的字段名
			whereJson: {
				pay_status: 1,
				_add_time: _.and(_.gt(last_StartTime), _.lt(last_EndTime))
			}
		});
		//查询ios昨日充值总金额
		res.all_last_jrczje_ios = await vk.baseDao.sum({
			dbName: "a_recharge",
			fieldName: "shifu_amount", // 需要求和的字段名
			whereJson: {
				os_name: "ios",
				pay_status: 1,
				_add_time: _.and(_.gt(last_StartTime), _.lt(last_EndTime))
			}
		});
		//查询android昨日充值总金额
		res.all_last_jrczje_android = await vk.baseDao.sum({
			dbName: "a_recharge",
			fieldName: "shifu_amount", // 需要求和的字段名
			whereJson: {
				os_name: "android",
				pay_status: 1,
				_add_time: _.and(_.gt(last_StartTime), _.lt(last_EndTime))
			}
		}
		);

		if (res.all_last_jrczje == 0) {
			console.log("查询昨日充值总金额");
			return res;
		}


		//查询昨日新增用户数
		res.all_last_num = await vk.baseDao.count({
			dbName: "uni-id-users",
			whereJson: {
				register_date: _.and(_.gt(last_StartTime), _.lt(last_EndTime))
			}
		});
		//查询昨日充值笔数
		res.all_last_jrczbs = await vk.baseDao.count({
			dbName: "a_recharge",
			whereJson: {
				pay_status: 1,
				_add_time: _.and(_.gt(last_StartTime), _.lt(last_EndTime))
			}
		});


		//查询昨日新用户充值总金额
		res.all_last_xyhczje = await vk.baseDao.sum({
			dbName: "a_recharge",
			fieldName: "shifu_amount", // 需要求和的字段名
			whereJson: {
				pay_status: 1,
				user_register_date: _.and(_.gt(last_StartTime), _.lt(last_EndTime)),
				_add_time: _.and(_.gt(last_StartTime), _.lt(last_EndTime))
			}
		});


		//====查询充值订单里新付费用户数====
		let xffyh = await db.collection('a_recharge')
			.aggregate()
			.match({
				user_register_date: _.and(_.gt(last_StartTime), _.lt(last_EndTime)),
				_add_time: _.and(_.gt(last_StartTime), _.lt(last_EndTime)),
				user_id: db.command.exists(true),
				pay_status: 1
			})
			.group({
				_id: null,
				user_count: $.addToSet('$user_id')
			})
			.end()
		//获取新付费用户数量
		res.all_last_xffyh = xffyh.data.length ? xffyh.data[0].user_count.length : 0
		//====计算新付费用户数结束====


		//====查询今日充值人数====
		let jrczrs = await db.collection('a_recharge')
			.aggregate()
			.match({
				_add_time: _.and(_.gt(last_StartTime), _.lt(last_EndTime)),
				pay_status: 1
			})
			.group({
				_id: null,
				user_count: $.addToSet('$user_id')
			})
			.end()
		res.all_last_jrczrs = jrczrs.data.length ? jrczrs.data[0].user_count.length : 0
		//====查询今日充值人数结束====


		//查询昨日回传笔数
		res.all_last_hcbs = await vk.baseDao.count({
			dbName: "jl_huichuanjilu",
			whereJson: {
				// ["result.code"]: 0, //0代表回传成功
				type: "active_pay",
				time: _.and(_.gt(last_StartTime), _.lt(last_EndTime))
			}
		});

		//查询昨日回传金额
		res.all_last_hcje = await vk.baseDao.sum({
			dbName: "jl_huichuanjilu",
			fieldName: "money", // 需要求和的字段名
			whereJson: {
				// ["result.code"]: 0, //0代表回传成功
				type: "active_pay",
				time: _.and(_.gt(last_StartTime), _.lt(last_EndTime))
			}
		});

		//计算各项值
		res.all_last_jrczje = vk.pubfn.toDecimal(res.all_last_jrczje / 100, 2); //今日充值金额
		res.all_last_xyhczje = vk.pubfn.toDecimal(res.all_last_xyhczje / 100, 2); //新用户充值金额
		res.all_last_xyhffl = vk.pubfn.toDecimal(res.all_last_xffyh / res.all_last_num * 100,
			2); //新用户付费率
		res.all_last_hcje = vk.pubfn.toDecimal(res.all_last_hcje / 100, 2); //回传金额

		//添加平台昨日总数据到数据库
		let add_all_datas = await db.collection('a_datas').add({
			date: yesterday,
			date_time: last_StartTime, //添加一个记录日期的时间戳，方便后面使用筛选日期功能
			drxh: 0, //当日消耗
			drcz: res.all_last_jrczje || 0, //当日充值金额
			drcz_ios: res.all_last_jrczje_ios || 0, //当日充值金额
			drcz_android: res.all_last_jrczje_android || 0, //当日充值金额
			jrroi: 0, //当日roi
			gzcb: 0, //关注成本
			xyhffl: res.all_last_xyhffl || 0, //新用户付费率
			xyhs: res.all_last_num || 0, //新用户数
			xyhczje: res.all_last_xyhczje || 0, //新用户充值金额
			xyhczrs: res.all_last_xffyh || 0, //新用户充值人数
			drczrs: res.all_last_jrczrs || 0, //当日充值人数
			drczbs: res.all_last_jrczbs || 0, //当日充值笔数
			hcbs: res.all_last_hcbs || 0, //回传笔数
			hcje: res.all_last_hcje || 0, //回传金额
			tfid: "0",
			admin_id: "001", //用户ID
			admin_username: "admin", //用户名
			type: 0, //0是平台总数据，1是账号级数据，2是链接级数据，3机构数据
			remark: "type=0,是平台总数据",
			_add_time: new Date().getTime(),
			_add_time_str: vk.pubfn.timeFormat(new Date(), "yyyy-MM-dd hh:mm:ss")
		})
		let updateAdmin = await vk.baseDao.update({
			dbName: "uni-id-users", // 表名
			whereJson: { // 条件
				username: "admin",
			},
			dataJson: { // 需要修改的数据
				isComputed: 1, //当天是否已经计算，0没有计算，1已经计算
			}
		});
		console.log("添加平台级数据结果=", add_all_datas);


		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}